/* 
1-引入http核心模块
2-创建http实例对象===创建服务器对象
3-事件监听请求
4-监听服务器是否开启
*/


const fs = require('fs')
const path = require('path')

const http = require('http')
const server = http.createServer()

server.on('request', (req, res) => {  //  只要发送请求就会触发
    console.log(1111111)
    const { url, method } = req  // {url:'/aa',method:'get'}
    res.setHeader('Content-Type', 'text/html;charset=utf8')

    if (url == '/' || url == '/index.html') {
        fs.readFile(path.join(__dirname, '/public/index.html'), 'utf8', (err, dataStr) => {
            if (err) console.log(err)
            res.end(dataStr)
        })
    } else if (url == '/api2') {
        // fs.readFile(path.join(__dirname, '/public/about.html'), 'utf8', (err, dataStr) => {
        //     if (err) console.log(err)
        //     res.end(dataStr)
        // })
        res.end('{id:1111111}')
    } else if (url == '/api') {
        res.end('{id:1,name:"jack"}')
    } else {
        res.end('404')
    }

})

server.listen(3000, () => {
    console.log('http:127.0.0.1:3000')
})